#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

int const maxn = 4007;
int a[maxn];
int f[maxn][maxn];
int last[maxn];
int id[maxn];
int len;
int n;

std::map<int, int> dis;

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin >> n;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		std::cin >> a[i];
		if (!dis.count(a[i])) {
			id[len] = a[i];
			dis[a[i]] = len++;
		}
	}
	for (int i = 1, x; i <= n; i++) {
		x = a[i];
		int tid = dis[x];
		for (int j = 0; j < len; j++) {
			f[i][j] = std::max(f[i][j], f[last[j]][tid] + 1);
			ans = std::max(ans, f[i][j]);
		}
		last[tid] = i;
	}
	std::cout << ans << "\n";
}

